/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FieldDetailPanel.java
 *
 * Created on Oct 6, 2010, 5:23:57 AM
 */

package com.celephais.objectGenerator;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 *
 * @author Eugene Shen
 */
public class FieldDetailPanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 5674531715786490744L;

	public enum FieldType {UNKNOWN, STRING, TEXT, INT, DOUBLE, BOOL, REF, CHILD};

	private ObjectDetailsPanel parent;
	private int index;

    /** Creates new form FieldDetailPanel */
    public FieldDetailPanel(ObjectDetailsPanel parent, int index) {
		super();
		this.parent = parent;
		setIndex(index);
        initComponents();
		additionalInitComponents();
    }

	@Override
	public void requestFocus() {
		fieldTypeBox.requestFocus();
	}

	public int getIndex() {
		return index;
	}
	public void setIndex(int idx) {
		this.index = idx;
	}

	public FieldType getType() {
		String type = fieldTypeBox.getSelectedItem().toString();
		if (type.equalsIgnoreCase("String")) {
			return FieldType.STRING;
		}
		else if (type.equalsIgnoreCase("Text")) {
			return FieldType.TEXT;
		}
		else if (type.equalsIgnoreCase("Integer")) {
			return FieldType.INT;
		}
		else if (type.equalsIgnoreCase("Double")) {
			return FieldType.DOUBLE;
		}
		else if (type.equalsIgnoreCase("Boolean")) {
			return FieldType.BOOL;
		}
		else if (type.equalsIgnoreCase("Reference")) {
			return FieldType.REF;
		}
		else if (type.equalsIgnoreCase("Child")) {
			return FieldType.CHILD;
		}
		else {
			// WTF?
			return FieldType.UNKNOWN;
		}
	}

	public String getFieldName() {
		return nameBox.getText();
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldTypeBox = new JComboBox();
        nameBox = new JTextField();
        removeBtn = new JButton();

        fieldTypeBox.setModel(new DefaultComboBoxModel(new String[] { "String", "Text", "Integer", "Double", "Boolean", "Reference", "Child (* Not Yet)" }));
        fieldTypeBox.setToolTipText("Select the Type of this Field");

        nameBox.setText("fieldName");
        nameBox.setToolTipText("Enter a name for this field.  camelCase is recommended.");

        removeBtn.setFont(new Font("Arial Black", 1, 10)); // NOI18N
        removeBtn.setForeground(new Color(204, 0, 51));
        removeBtn.setText("X");
        removeBtn.setToolTipText("Remove this field");

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldTypeBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(nameBox, GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(removeBtn))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(fieldTypeBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(nameBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(removeBtn))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void additionalInitComponents() {
		nameBox.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				// Too annoying.
//				if (!parent.validateFieldNames()) {
//					nameBox.requestFocus();
//				}
			}
		});

		removeBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.removeField(FieldDetailPanel.this);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JComboBox fieldTypeBox;
    private JTextField nameBox;
    private JButton removeBtn;
    // End of variables declaration//GEN-END:variables

}
